﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TomShane.Neoforce.Controls;
using TomShane.Neoforce.Importers;
using System.Threading;

namespace GameLib
{

    /// <summary>
    /// Contains the event arguments for the login authentication process
    /// </summary>
    public class LoginEventArgs : System.EventArgs
    {
        public string Message;
        public bool Validated;
        public LoginEventArgs(bool validated, string message)
        {
            Message = message;
            Validated = validated;
        }
    }

    /// <summary>
    /// Handles a login authenticaion event
    /// </summary>
    /// <param name="sender">Who called the event</param>
    /// <param name="args">What happened</param>
    public delegate void LoginAuthenticationEventHandler(object sender, LoginEventArgs args);

    /// <summary>
    /// Displays the user login and handles the login process
    /// </summary>
    public class LoginControl : Component
    {
        #region Embedded Controls

        #region Login Window

        Window loginWindow;
        Button btnLogin;
        TextBox txtUsername;
        TextBox txtPassword;
        Label lblUsername;
        Label lblPassword;
        CheckBox chkRememberLogin;

        #endregion //Login Window

        #region Login Message Window
        
        Label loginLabel;
        Window loginMessageWindow;

        #endregion//Login Message Window

        #endregion//Embedded Controls

        #region Event Variables

        public LoginEventArgs LoginArgs;
        public event LoginAuthenticationEventHandler OnLoginAuthenticationChanged;

        #endregion//Event Variables

        #region Initialization

        public override void Init()
        {
            base.Init();

            loginWindow = new Window(Manager);
            loginWindow.Init();
            loginWindow.Text = "Test";
            loginWindow.Width = 200;
            loginWindow.Height = 120;
            loginWindow.Movable = false;
            loginWindow.Resizable = false;
            loginWindow.BorderVisible = false;
            loginWindow.Left = Manager.Graphics.PreferredBackBufferWidth/2 - loginWindow.ClientWidth/2;
            loginWindow.Top = Manager.Graphics.PreferredBackBufferHeight/2 + loginWindow.ClientHeight;
            loginWindow.Visible = true;

            lblUsername = new Label(Manager);
            lblUsername.Text = "Username";
            lblUsername.Width = 180;
            lblUsername.Height = 10;
            lblUsername.Top = 5;
            lblUsername.Left = 10;
            lblUsername.Parent = loginWindow;

            txtUsername = new TextBox(Manager);
            txtUsername.Width = 180;
            txtUsername.Left = 10;
            txtUsername.Top = 20;
            txtUsername.Parent = loginWindow;

            lblPassword = new Label(Manager);
            lblPassword.Text = "Password";
            lblPassword.Left = 10;
            lblPassword.Top = 45;
            lblPassword.Parent = loginWindow;

            txtPassword = new TextBox(Manager);
            txtPassword.PasswordChar = '*';
            txtPassword.Width = 180;
            txtPassword.Left = 10;
            txtPassword.Top = 60;
            txtPassword.Parent = loginWindow;

            chkRememberLogin = new CheckBox(Manager);
            chkRememberLogin.Width = 120;
            chkRememberLogin.Height = 15;
            chkRememberLogin.Left = 10;
            chkRememberLogin.Text = "Remember Login";
            chkRememberLogin.Top = loginWindow.ClientHeight - chkRememberLogin.Height - 10;
            chkRememberLogin.Parent = loginWindow;

            btnLogin = new Button(Manager);
            btnLogin.Init();
            btnLogin.Text = "Login";
            btnLogin.Width = 50;
            btnLogin.Height = 25;
            btnLogin.Left = loginWindow.ClientWidth - btnLogin.Width -10;
            btnLogin.Top = loginWindow.ClientHeight - btnLogin.Height - 10;
            btnLogin.Anchor = Anchors.Bottom;
            btnLogin.Parent = loginWindow;
            btnLogin.Click += new MouseEventHandler(btnLogin_Click);
            Manager.Add(loginWindow);
        }

        #endregion//Initialization

        #region Events

        void btnLogin_Click(object sender, MouseEventArgs e)
        {
            loginMessageWindow = new Window(Manager);
            loginMessageWindow.Init();
            loginMessageWindow.Height = 25;
            loginMessageWindow.Width = 150;
            loginMessageWindow.Left = Manager.Graphics.PreferredBackBufferWidth/2 - loginMessageWindow.Width / 2;
            loginMessageWindow.Top = Manager.Graphics.PreferredBackBufferHeight/2 - loginMessageWindow.Height / 2;
            loginMessageWindow.Passive = true;
            loginMessageWindow.Movable = false;
            loginMessageWindow.CloseButtonVisible = false;
            loginMessageWindow.BorderVisible = false;
            loginMessageWindow.AutoScroll = false;
            loginMessageWindow.IconVisible = false;
            loginMessageWindow.Resizable = false;            
            loginMessageWindow.Text = "";
            loginMessageWindow.ShowModal();
            Manager.Add(loginMessageWindow);

            loginLabel = new Label(Manager);
            loginLabel.Width = loginMessageWindow.Width;
            loginLabel.Height = loginMessageWindow.Height;
            loginLabel.Alignment = Alignment.MiddleCenter;
            loginLabel.Text = "Logging in...";
            loginLabel.Parent = loginMessageWindow;
            DoLogin();
        }

        /// <summary>
        /// Closes the login window
        /// </summary>
        public void Close()
        {
            loginWindow.Close();
        }

        #endregion //Events

        #region Private Methods

        private void DoLogin()
        {
            //authenticate user
            OnLoginAuthenticationChanged(this, new LoginEventArgs(true, "Valid login"));
            loginLabel.Text = "Logged in!";
            Thread thdWait = new Thread(new ThreadStart(WaitAndClose));
            thdWait.Start();
        }

        private void WaitAndClose()
        {
            Thread.Sleep(2000);
            loginMessageWindow.Close();
        }

        #endregion//Private Methods

        #region Constructors

        /// <summary>
        /// Creates a new Login Control
        /// </summary>
        /// <param name="manager">The Game Manager</param>
        /// <param name="xPos">The XPosition the login window is displayed at</param>
        /// <param name="yPos">The YPosition the login window is displayed at</param>
        public LoginControl(Manager manager)
            :base(manager)
        {}

        #endregion//Constructors
    }
}
